package org.jjche.sys.modules.system.service;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import org.jjche.common.dto.RoleSmallDTO;
import org.jjche.common.vo.DataScopeVO;
import org.jjche.common.vo.UserVO;
import org.jjche.core.util.SecurityUtil;
import org.jjche.sys.modules.system.domain.DeptDO;
import org.jjche.sys.modules.system.enums.DataScopeEnum;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>数据权限服务实现</p>
 *
 * @author Zheng Jie
 * @version 1.0.8-SNAPSHOT
 * @since 2020-05-07
 */
@Service
@RequiredArgsConstructor
public class DataService {

    private final RoleService roleService;
    private final DeptService deptService;

    /**
     * <p>
     * 用户角色改变时需清理缓存
     * </p>
     *
     * @param user 用户
     * @return /
     */
    public DataScopeVO getDataScope(UserVO user) {
        DataScopeVO dataScopeVO = new DataScopeVO();
        //管理员
        if (user.getIsAdmin()) {
            dataScopeVO.setAll(true);
        } else {
            // 查询用户角色
            List<RoleSmallDTO> roleSet = roleService.findByUsersId(user.getId());
            // 获取对应的部门ID
            for (RoleSmallDTO role : roleSet) {
                DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
                //权限：全部，跳出
                if (dataScopeEnum == DataScopeEnum.DATA_SCOPE_ALL) {
                    dataScopeVO.setAll(true);
                    break;
                }
                switch (Objects.requireNonNull(dataScopeEnum)) {
                    case DATA_SCOPE_DEPT_AND_CHILD:
                        dataScopeVO.getDeptCodes().add(user.getDept().getCode());
                        dataScopeVO.setDeptChild(true);
                        break;
                    case DATA_SCOPE_DEPT:
                        dataScopeVO.getDeptCodes().add(user.getDept().getCode());
                        dataScopeVO.setDept(true);
                        break;
                    case DATA_SCOPE_SELF:
                        dataScopeVO.setSelf(true);
                        dataScopeVO.setUserId(user.getId());
                        dataScopeVO.setUserName(user.getUsername());
                        break;
                    case DATA_SCOPE_CUSTOM:
                        dataScopeVO.setDept(true);
                        dataScopeVO.getDeptCodes().addAll(getCustomize(role));
                        break;
                    default:
                }
            }
        }
        return dataScopeVO;
    }

    /**
     * 获取自定义的数据权限
     *
     * @param role 角色
     * @return 数据权限code
     */
    public Set<String> getCustomize(RoleSmallDTO role) {
        List<DeptDO> depts = deptService.findByRoleId(role.getId());
        Set<String> codeList = depts.stream().map(DeptDO::getCode).collect(Collectors.toSet());
        return codeList;
    }

    /**
     * <p>
     * 获取用户权限部门ids
     * </p>
     *
     * @return /
     */
    public Set<Long> listAuthDeptIds() {
        DataScopeVO dataScope = SecurityUtil.getUserDataScope();
        //是否限制部门
        boolean isDept = dataScope.isAll() == false && ((dataScope.isDept() || dataScope.isDeptChild()) && CollUtil.isNotEmpty(dataScope.getDeptCodes()));
        if (isDept) {
            return deptService.listIdsByCodes(dataScope.getDeptCodes());
        }
        return null;
    }
}
